<template>
  <div class="animated fadeIn">
    <div class="row">
      <div class="col-lg-12">
        <b-card header="<i class='fa fa-align-justify'></i> 展会信息">
          <div class="search-box">
            <b-button variant="outline-primary" @click="addExh()">添加展会信息</b-button>
          </div>
          <table class="table table-striped">
            <thead>
            <tr>
              <th>序号</th>
              <th>日期</th>
              <th>展会名称</th>
              <th>地点</th>
              <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr>
              <td colspan="5" v-show="isExh" style="text-align: center;">没有搜索到匹配的信息</td>
            </tr>
            <tr  v-for="(item, index) in currentData">
              <td>{{ index + 1 + number }}</td>
              <td>{{ item.title.substring(0, 20) }}</td>
              <td>{{ item.dates }}</td>
              <td>{{ item.venue }}</td>
              <td>
                <span class="badge badge-success" @click="findExhDetail(item.id)">详情</span>
                <span class="badge badge-success" @click="editExhDetail(item.id)">编辑</span>
                <span class="badge badge-danger" @click="delExh(item.id)">删除</span>
              </td>
            </tr>
            </tbody>
          </table>
          <el-pagination
            layout="prev, pager, next"
            :current-page.sync="current"
            :page-size="10"
            :total="total">
          </el-pagination>
        </b-card>
      </div>
    </div>

    <div title="Modal title" class="modal-primary" v-model="primaryModal" v-show="primaryModal">
      <span class="modal-open">
        <div class="modal fade show" role="dialog">
          <div class="modal-dialog modal-md">
            <div tabindex="-1" role="document" class="modal-content">
              <header class="modal-header">
                <h5 class="modal-title">展会信息</h5>
                <button type="button" aria-label="Close" class="close" @click="cancelDel">
                  <span aria-hidden="true">×</span>
                </button>
              </header>
              <div class="modal-body">
                <br>是否删除该条展会信息？<br><br>
              </div>
              <footer class="modal-footer">
                <button type="button" class="btn btn-primary btn-md" @click="cancelDel()">取消</button>
                <button type="button" class="btn btn-danger btn-md" @click="sureDel()">确认</button>
              </footer>
            </div>
          </div>
        </div>
        <div class="modal-backdrop fade show"></div>
      </span>
    </div>
  </div>
</template>

<script>
  import { findAllEvent, deleteEvent } from '../../service/api'
  import _ from 'lodash'
  export default {
    name: 'tables',
    data () {
      return {
        delId: '',
        current: 1,
        number: 0,
        datas: [],
        primaryModal: false,
        isExh: false,
        startDate: '',
        endDate: '',
        modalOpen: false,
        pageNumber: ''
      }
    },
    computed: {
      currentData () {
        return this.datas[this.current - 1]
      },
      total () {
        // return this.datas.length
        return ~~this.pageNumber
      }
    },
    methods: {
      // 查看新闻详情
      findExhDetail (index) {
        this.$router.push({
          path: '/news/e_detail?flag=1&id=' + index
        })
      },
      // 编辑新闻信息
      editExhDetail (index) {
        this.$router.push({
          path: '/news/e_detail?flag=2&id=' + index
        })
      },
      // 添加新闻信息
      addExh () {
        this.$router.push({
          path: '/news/e_detail?flag=3'
        })
      },
      // 删除新闻信息
      delExh (index) {
        this.modalOpen = true
        this.primaryModal = true
        this.delId = index
      },
      cancelDel () {
        this.modalOpen = false
        this.primaryModal = false
      },
      sureDel () {
        this.modalOpen = false
        this.primaryModal = false
        deleteEvent({id: this.delId})
          .then(({ data, err }) => {
            console.log('err: ' + err)
            console.log('data: ' + data)
            if (data.code === 200) {
              // 页面刷新
              alert('删除成功！')
              location.reload()
            }
          })
      },
      handleCurrentChange () {
        if (this.current === 1) {
          this.number = 0
        } else {
          this.number = (this.current - 1) * 10
        }
      }
    },
    created () {
      // fetch
      findAllEvent()
        .then(({ data, err }) => {
          console.log('err: ' + err)
          console.log('data: ' + data)
          if (data.code === 200) {
            this.datas = _.chunk(data.result, 10)
            this.pageNumber = data.result.length
            if (data.result.length === 0) {
              this.isExh = true
            } else {
              this.isExh = false
            }
          }
        })
    }
  }
</script>
<style>
  .badge{
    cursor: pointer;
  }
  .search-box{
    vertical-align: top;
    margin-bottom: 20px;
  }
  .search-box div{
    display: inline-block;
    line-height: 36px;
    height: 36px;
  }
  .search-box .btn-search{
    vertical-align: top;
    margin-left: 10px;
  }
  .search-box input{
    vertical-align: top;
    display: inline-block;
    height: 35px;
    /*width: 250px;*/
  }
  .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: auto;
  }
  .search-info {
    margin-left: 15px;
    width: 250px;
  }
  .data-time{
    width: auto;
    height:35px;
  }
</style>
